package com.demo.security.cloud.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;

public class EncryptUtil {

    public static String encodeBase64(byte[] bytes){

        return Base64.getEncoder().encodeToString(bytes);
    }

    public static byte[] decodeBase64(String str){
        byte[] bytes = null;
        return Base64.getDecoder().decode(str);
    }

    public static String encodeUTF8StringBase64(String str){
        String encode = null;
        try {
            encode = Base64.getEncoder().encodeToString(str.getBytes("utf-8"));
        }catch (UnsupportedEncodingException e){

        }
        return encode;
    }

    public static String decodeUTF8StringBase64(String str){
        String decode = null;
        byte[] bytes = Base64.getDecoder().decode(str);
        try {
            decode = new String(bytes, "utf-8");
        }catch (UnsupportedEncodingException e){

        }
        return decode;
    }

    public static String encodeURL(String url){
        String encode = null;
        try {
            encode = URLEncoder.encode(url, "utf-8");
        }catch (UnsupportedEncodingException e){

        }
        return encode;
    }

    public static String decodeURL(String url){
        String encode = null;
        try {
            encode = URLEncoder.encode(url, "utf-8");
        }catch (UnsupportedEncodingException e){

        }
        return encode;
    }
}
